//
//  FDWeatherView.m
//  FamousDoctorOnline
//
//  Created by wszyxc on 16/7/5.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "FDWeatherView.h"

#import "FDProvinceViewController.h"
#import "FDCityViewController.h"
#import "UIButton+FDExtension.h"
#import "FDWeatherModel.h"
#import "FDProvinceModel.h"

#import "UIImageView+WebCache.h"

#define  kNavMaxY  64
#define  kWeatherH 70

@interface FDWeatherView ()

@property (weak,nonatomic) UIImageView * weatherImageView;

@property (weak,nonatomic) UIView * lineView;

@property (weak,nonatomic) UILabel * weatherLabel;
//Label换成Button
//@property (weak,nonatomic) UILabel * addrLabel;
//天气按钮
@property (weak,nonatomic) UIButton * addrButton;

@property (weak,nonatomic) UILabel * tLabel;

@property (weak,nonatomic) UILabel * dateLabel;

//当天的天气
@property (nonatomic, strong) FDWeatherModel *curWeather;

@end

@implementation FDWeatherView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
        
    }
    
    return self;
}

/**
 *  布局子控件
 */
- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    [self setupWeatherImageViewLayout];
    
    [self setupLineViewLayout];
    
    [self setupWeatherLabelLayout];
    //Label换成Button
    //    [self setupAddrLabelLayout];
    //按钮约束
    [self setupAddrButtonLayout];
    
    [self setupTLabelLayout];
    
    [self setupDateLabelLayout];
    
}


- (void)setupUI{
    
    self.backgroundColor = [UIColor colorWithRed:168.0/255 green:240.0/255 blue:219.0/255 alpha:1.0];

    
    //添加天气图标
    UIImageView *weatherImageView = [[UIImageView alloc]init];
    
    self.weatherImageView = weatherImageView;
    
    weatherImageView.image = [UIImage imageNamed:@"sun"];
    
    [self addSubview:weatherImageView];

    //添加中间竖线view
    UIView *lineView = [[UIView alloc]init];
    
    self.lineView = lineView;
    
    [self addSubview:lineView];
    
    lineView.backgroundColor = [UIColor grayColor];
    

    //添加天气label
    UILabel *weatherLabel = [self setupOneLabelWithTitle:@"晴" testColor:kThemeColor fontSize:SCREEN_WIDTH*0.04];
    
    weatherLabel.numberOfLines = 0;
    
    self.weatherLabel = weatherLabel;
    
    [self addSubview:weatherLabel];
    
 
    
    //添加地点label 改 Label换成Button
    //    UILabel *addrLabel = [self setupOneLabelWithTitle:@"上海" testColor:kThemeColor fontSize:18];
    //
    //    self.addrLabel = addrLabel;
    //
    //    [self addSubview:addrLabel];
    
    //添加地点改Button
    UIButton *addrButton = [UIButton setupOneButtonWithTitle:@"上海"];
    
    //单击事件
    [addrButton addTarget:self action:@selector(clickProvinceButton) forControlEvents:UIControlEventTouchUpInside];
    
    addrButton.titleLabel.numberOfLines = 0;
    
    self.addrButton = addrButton;
    
    [self addSubview:addrButton];

    
    //添加温度label
    UILabel *tLabel = [self setupOneLabelWithTitle:@"温度: 24~30" testColor:kThemeColor fontSize:18];
    
    self.tLabel = tLabel;
    
    tLabel.textAlignment = NSTextAlignmentCenter;
    
    [self addSubview:tLabel];
    
    
    //添加日期label
    UILabel *dateLabel = [self setupOneLabelWithTitle:@"日期: 周一 06月27日" testColor:kThemeColor fontSize:18];
    
    self.dateLabel = dateLabel;
    
    dateLabel.textAlignment = NSTextAlignmentCenter;
    
    [self addSubview:dateLabel];
    
}

- (void)setupWeatherImageViewLayout{
    
    [self.weatherImageView mas_makeConstraints:^(MASConstraintMaker *make) {
    
            make.width.height.offset(40);
            make.leading.offset(10);
    
            make.centerY.equalTo(self.mas_centerY);
            
        }];
    
}

- (void)setupLineViewLayout{
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        make.width.offset(1);
        make.height.offset(kWeatherH-15);
        
    }];
    
}

- (void)setupWeatherLabelLayout{
    
        [self.weatherLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    
            make.centerY.equalTo(self.mas_centerY);
    
            make.leading.equalTo(self.weatherImageView.mas_trailing).offset(5);
    
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH*0.15, SCREEN_WIDTH*0.2));
        }];
    
}

//Label换成Button
//- (void)setupAddrLabelLayout{
//
//    [self.addrLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.centerY.equalTo(self.mas_centerY);
//
//        make.trailing.equalTo(self.lineView.mas_leading).offset(-20);
//
//    }];
//
//}

//Button约束
- (void)setupAddrButtonLayout{
    
    [self.addrButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.mas_centerY);
        
        make.leading.equalTo(self.weatherLabel.mas_trailing).offset(-5);
        
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH*0.2, SCREEN_WIDTH*0.2));
        
    }];
}

- (void)setupTLabelLayout{
    
    [self.tLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.lineView.mas_trailing).offset(0);
        make.trailing.offset(0);
        
        make.top.offset(10);
        
    }];
}

- (void)setupDateLabelLayout{
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.lineView.mas_trailing).offset(0);
        make.trailing.offset(0);
        
        make.bottom.offset(-10);
        
    }];
    
}


- (UILabel *)setupOneLabelWithTitle:(NSString *)title testColor:(UIColor *)color fontSize:(CGFloat)fontSize{
    
    UILabel *label = [[UILabel alloc]init];
    
    label.textColor = color;
    
    label.font = [UIFont systemFontOfSize:fontSize];
    
    label.text = title;
    
    return label;
    
}

//快速创建button
//- (UIButton *)setupOneButtonWithTitle:(NSString *)title testColor:(UIColor *)color fontSize:(CGFloat)fontSize{
//    
//    UIButton *button = [[UIButton alloc]init];
//    
//    [button setTitleColor:color forState:UIControlStateNormal];
//    
//    button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
//    
//    [button setTitle:title forState:UIControlStateNormal];
//   
//    button.titleLabel.numberOfLines = 0;
//    
//    NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
//    
//    mDict[NSUnderlineStyleAttributeName] = @1;
//    mDict[NSForegroundColorAttributeName] = kThemeColor;
//    
//    return button;
//    
//}

//点击按钮事件
- (void)clickProvinceButton{

    if (self.selectCityBlock) {
        _selectCityBlock();
    }
}

/**
 *  重写datalist的set方法
 */
- (void)setDataList:(NSArray<FDWeatherModel *> *)dataList{

    _dataList = dataList;
    
    //获取系统日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateStyle:NSDateFormatterFullStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    
    NSString *number =  [[[dateFormatter stringFromDate:[NSDate date]] componentsSeparatedByString:@"期"] lastObject];
    
    NSString *weekStr = [NSString stringWithFormat:@"周%@",number];
    
    NSString *dayStr = dataList[0].date;
    
    NSString *string = [[dayStr componentsSeparatedByString:@" "]firstObject];
    
    //判断日期
    if ([weekStr isEqualToString:string]) {
        self.curWeather = dataList[0];
    }else{
        self.curWeather = dataList[1];
    }

    NSMutableArray *temp = dataList.mutableCopy;
    [temp removeObjectAtIndex:0];

}

/**
 *  重写model的set方法
 */
- (void)setCurWeather:(FDWeatherModel *)curWeather{

    _curWeather = curWeather;
    
    //取出当前小时
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH"];
    
    NSString *str = [formatter stringFromDate:[NSDate date]];
    
    int time = [str intValue];
    
    //判断白天和晚上
    if (time>=18||time<=06) {
        [self.weatherImageView sd_setImageWithURL:[NSURL URLWithString:curWeather.nightPictureUrl]];
    }
    else{
        [self.weatherImageView sd_setImageWithURL:[NSURL URLWithString:curWeather.dayPictureUrl]];
    }
    
    //控件赋值
    self.weatherLabel.text = curWeather.weather;
    
    [self.addrButton setTitle:[NSString stringWithFormat:@"%@",curWeather.currentCity] forState:UIControlStateNormal];
    
    NSString *title = self.curWeather.currentCity;
    //获取不到数据时
    if (!title) {
        title = @"无数据";
    }
    
    //按钮下划线
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:title];
    
    NSRange strRange = {0,[attrStr length]};
    
    NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
    
    mDict[NSUnderlineStyleAttributeName] = @1;
    mDict[NSForegroundColorAttributeName] = kThemeColor;
    
    [attrStr addAttributes:mDict range:strRange];
    
    [self.addrButton setAttributedTitle:attrStr forState:UIControlStateNormal];
    
    self.tLabel.text = [NSString stringWithFormat:@"温度: %@",curWeather.temperature];
    
    NSString *dataString = curWeather.date;
    
    dataString = [[dataString componentsSeparatedByString:@"("]firstObject];
    
    self.dateLabel.text = [NSString stringWithFormat:@"%@",dataString];
}

@end
